#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>

void Print(int* arr, int len)
{
	int i = 0;
	for (i = 0; i < len; i++)
	{
		printf("%d ", arr[i]);
	}
	printf("\n");
}

void ShellSort(int* arr, int len)
{
	int gap = len;
	while (gap > 1)
	{
		gap = gap / 3 + 1;
		for (int i = 0; i < len - gap; i++)
		{
			int end = i;
			int stander = arr[end + gap];
			while (end >= 0)
			{
				if (arr[end] > stander)
				{
					arr[end + gap] = arr[end];
					end -= gap;
				}
				else 
				{
					break;
				}
			}
			arr[end + gap] = stander;
		}
	}
}

void Swap(int* p1, int* p2)
{
	int t = *p1;
	*p1 = *p2;
	*p2 = t;
}

int partsort1(int* arr, int left, int right)
{
	int stander = left;
	while (left < right)
	{
		while (left < right && arr[right] >= arr[stander])
		{
			right--;
		}
		while (left < right && arr[left] <= arr[stander])
		{
			left++;
		}
		Swap(arr + left, arr + right);
	}
	Swap(arr + stander, arr + left);
	return left;
}

int partsort2(int* arr, int left, int right)
{
	int hole = left;
	int temp = arr[left];
	while (left < right)
	{
		while (left < right && arr[right] >= temp)
		{
			right--;
		}
		arr[hole] = arr[right];
		hole = right;

		while (left < right && arr[left] <= temp)
		{
			left++;
		}
		arr[hole] = arr[left];
		hole = left;
	}
	Swap(&temp, arr + left);
	return hole;
}

int partsort3(int* arr, int left, int right)
{
	int cur = left;
	int prev = left + 1;
	while (prev <= right)
	{
		if (arr[prev] < arr[left])
		{
			cur++;
			if (cur != prev)
				Swap(arr + cur, arr + prev);
		}
		prev++;
	}
	Swap(arr + left, arr + cur);
	return cur;
}

void QuickSort(int* arr, int left, int right)
{
	if (left >= right)
		return;
	int midi = partsort3(arr, left, right);

	QuickSort(arr, left, midi - 1);
	QuickSort(arr, midi + 1, right);
}

int main()
{
	int arr[] = { 7,8,9,4,5,6,1,2,3,0 };
	int len = sizeof(arr) / sizeof(arr[0]);

	//ShellSort(arr, len);
	QuickSort(arr, 0, len - 1);
	Print(arr, len);

	return 0;
}